var IqcReportCheckType = function (type, data) {
  BaseModel.call(this);
  var self = this;

  var fn = {
    initData: function (type, data) {
      if (type === 'create') {
        this.createCheckType(data);
      } else {
        this.initCheckType(data);
      }
    },

    createCheckType: function (data) {
      self.projectName = data.checkType;
      self.checkDesc = data.checkDesc;
      self.checkMethod = '';
      self.samplingStandard = '';
      self.checkResult = '';
      self.remark = '';
    },

    initCheckType: function (data) {
      self.projectName = data.projectName;
      self.checkDesc = data.checkDesc;
      self.checkMethod = data.checkMethod;
      self.samplingStandard = data.samplingStandard;
      self.checkResult = data.checkResult;
      self.remark = data.remark;
    }
  };

  this.changePropery = function (property, value) {
    this[property] = value;
  };

  this.checkValid = function () {
    var fields = ['checkMethod', 'samplingStandard', 'checkResult'];
    return this.checkRequiredFields(fields);
  };

  this.getObject = function () {
    let postData = {
      projectName: this.projectName,
      checkDesc: this.checkDesc,
      checkMethod: this.checkMethod,
      samplingStandard: this.samplingStandard,
      checkResult: this.checkResult,
      conclusion: this.conclusion,
      remark: this.remark
    };

    return postData;
  };

  fn.initData(type, data);
};

var IqcReportModel = function (type, data) {
  BaseModel.call(this);
  var self = this;

  var fn = {
    initData: function (type, data) {
      if (type === 'add') {
        this.createReport(data);
      } else if (type === 'approval') {
        this.initReport(data);
      }
    },

    createReport: function (data) {
      var material = data.material;
      self.purhaseId = material.purchaseId;
      self.materialName = material.materialName;
      self.materialType = material.materialType;
      self.providerName = material.providerName;
      self.companyOrderId = material.companyOrderId;
      self.factoryNo = material.factoryNo;
      self.purchaseCount = material.purchaseCount;

      self.isNeedApproval = '';
      self.totalCount = material.purchaseCount - material.countb;

      var materialCheckTypeList = data.materialCheckTypeList;
      materialCheckTypeList.unshift({
        checkType: "规格",
        checkDesc: material.model
      }, {
        checkType: "编号",
        checkDesc: material.color
      }, {
        checkType: "描述",
        checkDesc: material.colorDescription
      }, {
        checkType: "供方编号",
        checkDesc: material.providerNo
      }, {
        checkType: "材质",
        checkDesc: material.material
      });
      var checkTypeList = [];
      for (var i = 0; i < materialCheckTypeList.length; i++) {
        var checkType = materialCheckTypeList[i];
        checkType = new IqcReportCheckType('create', checkType);
        checkTypeList.push(checkType);
      }
      self.checkTypeList = checkTypeList;

      self.evaluateDesc = '';
      self.conclusion = '';
      self.qualityCount = undefined;
      self.noQualityCount = undefined;
    },

    initReport: function (data) {
      var reportMain = data.reportMain;
      self.checkReportNo = reportMain.checkReportNo;
      self.purhaseId = reportMain.purhaseId;
      self.materialName = reportMain.materialName;
      self.materialType = reportMain.materialType;
      self.providerName = reportMain.providerName;
      self.companyOrderId = reportMain.companyOrderId;
      self.factoryNo = reportMain.factoryId;
      self.purchaseCount = reportMain.purchaseCount;
      self.isNeedApproval = reportMain.isNeedApproval;

      self.evaluateDesc = reportMain.evaluateDesc;
      self.conclusion = reportMain.conclusion;
      self.qualityCount = reportMain.qualityCount;
      self.noQualityCount = reportMain.noQualityCount;

      var materialCheckTypeList = data.materialCheckTypeList;
      var checkTypeList = [];
      for (var i = 0; i < materialCheckTypeList.length; i++) {
        var checkType = materialCheckTypeList[i];
        checkType = new IqcReportCheckType('init', checkType);
        checkTypeList.push(checkType);
      }
      self.checkTypeList = checkTypeList;
    }
  };

  this.changeAllProperty = function (field, value) {
    value = Number(value);
    var checkTypeList = this.checkTypeList;
    for (var i = 0; i < checkTypeList.length; i++) {
      var checkType = checkTypeList[i];
      checkType.changePropery(field, value);
    }
  };

  this.checkValid = function () {
    if (this.isNeedApproval === '') {
      return 'isNeedApproval';
    }

    var checkTypeList = this.checkTypeList;
    for (var i = 0; i < checkTypeList.length; i++) {
      var checkType = checkTypeList[i];
      var code = checkType.checkValid();
      if (code !== '') {
        return [checkType.projectName, code];
      }
    }

    if (this.conclusion === '') {
      return 'conclusion';
    }

    var qualityCount = this.qualityCount || 0;
    var noQualityCount = this.noQualityCount || 0;
    var totalCount = this.totalCount;
    if (qualityCount + noQualityCount > totalCount) {
      return ['良品数量+不良品数量之和不得大于采购未检测数' + totalCount];
    }
    return '';
  };

  this.getObject = function () {
    var postData = {
      purhaseId: this.purhaseId,
      isNeedApproval: this.isNeedApproval,
      evaluateDesc: this.evaluateDesc,
      conclusion: this.conclusion,
      qualityCount: this.qualityCount,
      noQualityCount: this.noQualityCount
    };

    var checkArray = [];
    var checkTypeList = this.checkTypeList;
    for (var i = 0; i < checkTypeList.length; i++) {
      var checkType = checkTypeList[i];
      checkType = checkType.getObject();
      checkArray.push(checkType);
    }
    postData.checkArray = JSON.stringify(checkArray);

    return postData;
  };

  fn.initData(type, data);
};
